package com.itheima.health.service.impl;

import com.itheima.health.mapper.TongjiMapper;
import com.itheima.health.model.vos.*;
import com.itheima.health.service.TongjiService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Service
public class TongjiServiceImpl implements TongjiService {
    @Autowired
    private TongjiMapper tongjiMapper;

    @Override
    public tongjiVo getMemberReport() {
        // 从数据库查
        List<TongjishijianVo> tongjis = tongjiMapper.getMemberReport();
        if (CollectionUtils.isEmpty(tongjis)) {
            return new tongjiVo();
        }
        List<String> months = new ArrayList<>();
        List<Integer> memberCount = new ArrayList<>();

        for (TongjishijianVo tongji : tongjis) {
            months.add(tongji.getName());
            memberCount.add(tongji.getId());
        }
        tongjiVo vo = new tongjiVo();
        vo.setMonths(months);
        vo.setMemberCount(memberCount);
        return vo;

    }

    @Override
    public TongjiTaocaiVo getSetmealReport() {
        TongjiTaocaiVo vo = new TongjiTaocaiVo();
        vo.setSetmealCount(tongjiMapper.getSetmealReport());
        List<String> st = new ArrayList<>();
        for (TongjishujuVo tongjishujuVo : vo.getSetmealCount()) {
            st.add(tongjishujuVo.getName());
        }

        vo.setSetmealNames(st);
        return vo;

    }

    @Override
    public TongjiyemianVo Business() {
        TongjiyemianVo vo = tongjiMapper.Business();
        List<TongjiyemianzhanshiVo> list = tongjiMapper.selectHotSetMeal();
        for (TongjiyemianzhanshiVo hotSetMealDto : list) {
            if (hotSetMealDto.getSetmealCount() != null) {
                Integer setmealCount = hotSetMealDto.getSetmealCount();
                BigDecimal bigDecimal1 = new BigDecimal(setmealCount);
                BigDecimal bigDecimal2 = new BigDecimal(list.size());
                BigDecimal divide = bigDecimal1.divide(bigDecimal2);
                hotSetMealDto.setProportion(divide);
                hotSetMealDto.setSetmeal_count(hotSetMealDto.getSetmealCount());
            } else {
                hotSetMealDto.setProportion(new BigDecimal(0));
            }
        }
        return vo;
    }

    @Override
    public void exportBusinessReport(HttpServletResponse response) throws Exception {
        // 下载excel
        String fileName = "统计分析.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

        // 加载模板
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("xlsx/report_template.xlsx");
        // 输出到response
       /* ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
                .withTemplate(inputStream)
                .excelType(ExcelTypeEnum.XLSX).build();

        // 得到sheet
        WriteSheet writeSheet = EasyExcel.writerSheet().build();

        // 单个字段填充
        OperationDataVO businessReportData = getBusinessReportData();
        List<HotSetmealVO> hotSetmeal = businessReportData.getHotSetmeal();
        excelWriter.fill(businessReportData, writeSheet);
        excelWriter.fill(new FillWrapper("hotSetmeal", hotSetmeal), writeSheet);
        excelWriter.finish();*/
    }
}



